package BackTrackingPackage;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author Lzm
 * @version 1.0
 */
public class solveSudoku_ {
@Test
public void solveSudokuTest(){
  char[][] chessboard = new char[9][9];
  for (int i = 0 ; i < 9 ; i ++){
    Arrays.fill(chessboard[i],'.');
  }
}
  public void backTracking(char[][] board){
      if (isSuccess(board,'.')){
        return;
      }
      for (int i = 0 ; i < board.length ; i ++){
        for (int j = 0 ; j < board[i].length ; j ++){
          if (board[i][j] != '.'){
            continue;
          }
          else{
            for (char k = '1' ; k <= '9' ; k ++){
              if (isValid(board,i,j,k)){
                board[i][j] = k;
              }
            }
          }
        }
      }
  }
  /*检查在board上, 在row行,col列填入number是否合法*/
  public boolean isValid(char[][] board,int row,int col,char number){
    /*检查行*/
    for (int i= 0 ; i < board.length ; i ++){
      if (board[row][i] == '.'){
        continue;
      }
      else{
        if (number == board[row][i]){
          return false;
        }
      }
    }
    /*检查列*/
    for (int i = 0 ; i < board.length ; i++){
      if (board[i][col] == '.'){
        continue;
      }
      else{
        if (number == board[i][col]){
          return false;
        }
      }
    }
    /*检查3*3宫*/
    for (int i = (row / 3) * 3 ; i < (row / 3) * 3 + 3;i++){
      for (int j = (col / 3) * 3;j < (col / 3) * 3 + 3 ; j++){
        if (board[i][j] == '.'){
          continue;
        }
        else{
          if (number == board[i][j]){
            return false;
          }
        }
      }
    }
    return true;
  }
  public boolean isSuccess(char[][] board,char character){
    for (int i = 0 ; i < board.length ; i ++){
      for (int j = 0 ; i < board[i].length ; j++){
        if (board[i][j] == character){
          return false;
        }
      }
    }
    return true;
  }
}
